package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GweaT;
import cn.e4j.modular.crm.mapper.GweaTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 税率税种 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-09
 */
@Service
public class Gwei001ServiceImpl extends ServiceImpl<GweaTMapper, GweaT>{

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GweaT gweaT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        QueryWrapper<GweaT> gweaTQueryWrapper = new QueryWrapper<>();
        gweaTQueryWrapper.eq("GWEAENT",shiroUser.getDBC());
        gweaTQueryWrapper.eq("GWEA001",gweaT.getGWEA001());
        return this.baseMapper.selectList(gweaTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GweaT gweaT){
        gweaT.setGWEA005(new BigDecimal("1"));//1:应税 2：零税 3：免税
        gweaT.setGWEAACTI("Y");
        gweaT.insert();
    }

    @Transactional
    public void editData(GweaT gweaT,GweaT oldGweaT){
        UpdateWrapper<GweaT> gweaTUpdateWrapper = new UpdateWrapper<>();
        gweaTUpdateWrapper.eq("GWEAENT",oldGweaT.getGWEAENT());
        gweaTUpdateWrapper.eq("GWEA001",oldGweaT.getGWEA001());
        gweaT.update(gweaTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GweaT gweaT){
        delete(gweaT);
    }

    @Transactional
    public void deleteDataBatch(List<GweaT> gweaTList){
        for(GweaT gweaT : gweaTList){
            delete(gweaT);
        }
    }

    private void delete(GweaT gweaT){
        UpdateWrapper<GweaT> gweaTUpdateWrapper = new UpdateWrapper<>();
        gweaTUpdateWrapper.eq("GWEAENT",gweaT.getGWEAENT());
        gweaTUpdateWrapper.eq("GWEA001",gweaT.getGWEA001());
        gweaT.delete(gweaTUpdateWrapper);
    }
}
